Page({
  data: {
    healthRecord: {
      name: '张三',
      gender: '男',
      birthDate: '1990-01-01',
      height: 175,
      weight: 70,
      bloodPressure: '120/80mmHg',
      pastIllnesses: '无'
    }
  },
  onShow() {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    if (currentPage.data.healthRecord) {
      this.setData({
        healthRecord: currentPage.data.healthRecord
      });
    }
  },
  editHealthRecord() {
    const healthRecord = JSON.stringify(this.data.healthRecord);
    wx.navigateTo({
      url: `/pages/Health_record/Health_record?healthRecord=${healthRecord}`
    });
  },
  onLoad(options) {
    const healthRecordStr = options.healthRecord;
    if (healthRecordStr) {
      try {
        const healthRecord = JSON.parse(healthRecordStr);
        this.setData({
          healthRecord
        });
      } catch (error) {
        console.error('解析健康档案数据时出错:', error);
      }
    }
  },
  inputName(e) {
    const { healthRecord } = this.data;
    healthRecord.name = e.detail.value;
    this.setData({
      healthRecord
    });
  },
  inputGender(e) {
    const { healthRecord } = this.data;
    healthRecord.gender = e.detail.value;
    this.setData({
      healthRecord
    });
  },
  inputBirthDate(e) {
    const { healthRecord } = this.data;
    healthRecord.birthDate = e.detail.value;
    this.setData({
      healthRecord
    });
  },
  inputHeight(e) {
    const { healthRecord } = this.data;
    // 将输入值转换为数值类型
    healthRecord.height = parseFloat(e.detail.value) || 0;
    this.setData({
      healthRecord
    });
  },
  inputWeight(e) {
    const { healthRecord } = this.data;
    // 将输入值转换为数值类型
    healthRecord.weight = parseFloat(e.detail.value) || 0;
    this.setData({
      healthRecord
    });
  },
  inputBloodPressure(e) {
    const { healthRecord } = this.data;
    healthRecord.bloodPressure = e.detail.value;
    this.setData({
      healthRecord
    });
  },
  inputPastIllnesses(e) {
    const { healthRecord } = this.data;
    healthRecord.pastIllnesses = e.detail.value;
    this.setData({
      healthRecord
    });
  },
  saveHealthRecord() {
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; // 获取上一页
    if (prevPage) {
      prevPage.setData({
        healthRecord: this.data.healthRecord
      });
      // 显示保存成功提示
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 2000
      });
    }
  },
  cancelEdit() {
    wx.navigateBack({
      delta: 1
    });
  }
})    